package com.efamily.ewallet.client.profit.bean;

import java.util.Date;

import com.efamily.ewallet.client.exception.DayConvertException;
import com.efamily.ewallet.client.exception.MonthConvertException;
import com.efamily.ewallet.client.utils.Utils;
import com.extjs.gxt.ui.client.data.BaseModelData;

public class ProfitBean extends BaseModelData {

	public ProfitBean() {

	}

	public ProfitBean(int id, ProfitTypeBean type, Date profitDate,
			double value, String description) {
		setId(id);
		setType(type);
		setProfitDate(profitDate);
		setValue(value);
		setDescription(description);
	}

	public int getId() {
		return get("id", -1);
	}

	public void setId(int id) {
		set("id", id);
	}

	public ProfitTypeBean getType() {
		return get("type");
	}

	public void setType(ProfitTypeBean type) {
		set("type", type);
	}

	public Date getProfitDate() {
		return get("profit_date");
	}

	@SuppressWarnings("deprecation")
	public void setProfitDate(Date profitDate) {
		try {
			set("week_day", Utils.intToDayName(profitDate.getDay()));
			set("month", Utils.intToMonthName(profitDate.getMonth()));
		} catch (DayConvertException e) {
			set("week_day", "unknown");
		} catch (MonthConvertException e) {
			set("month", "unknown");
		}
		set("month_day", profitDate.getDate());
		set("year", 1900 + profitDate.getYear());
		set("profit_date", profitDate);
	}

	public double getValue() {
		return get("value", 0.0);
	}

	public void setValue(double value) {
		set("value", value);
	}

	public String getDescription() {
		return get("description");
	}

	public void setDescription(String description) {
		set("description", description);
	}

	@Override
	public int hashCode() {
		if (getId() != -1) {
			return getId();
		} else {
			return super.hashCode();
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (obj == this)
			return true;
		if (obj.getClass() != getClass())
			return false;

		return (((ProfitBean) obj).getId() == this.getId());

	}

}
